package com.ytjj.qmyx.admin.controller.shortChainPlatform;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.model.request.dl.DlList;
import com.ytjj.qmyx.admin.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 短链平台管理
 */
@Slf4j
@RefreshScope
@RestController
@RequestMapping("/dl")
public class ShortChainPlatform {

    @Value("${short.platfrom.url:}")
    private String url;

    /**
     * 查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public CommonResult findPage(Integer pageNum,Integer pageSize,String keyword){
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf(pageNum));
        map.put("size",String.valueOf(pageSize));
        map.put("keyword",keyword);
        String resultJson = HttpClientUtil.doGet(url+"/admin/findPage",map);
        Map data = JSON.parseObject(resultJson, Map.class);
        return CommonResult.success(data);
    }

    /**
     * 新增
     * @param dlList
     * @return
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody DlList dlList){
        String name = UserUtil.getAdminData().getCompellation();
        dlList.setCreateAdmin(name);
        String resultJson = HttpClientUtil.doPostJson(url+"/admin/add", JSON.toJSONString(dlList));
        return CommonResult.success();
    }

    /**
     * 修改
     * @param dlList
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody DlList dlList){
        String name = UserUtil.getAdminData().getCompellation();
        dlList.setUpdateAdmin(name);
        String resultJson = HttpClientUtil.doPostJson(url+"/admin/update", JSON.toJSONString(dlList));
        return CommonResult.success();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/remove/{id}")
    public CommonResult delete(@PathVariable Integer id){
        Map<String, Object> param = new HashMap<>();
        param.put("id",id);
        String result= HttpUtil.get(url+"/admin/delete",param);
        if (result != null && result.equals("ok")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
